package com.cinema.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cinema.Bean.Ticket;
import com.cinema.Bean.User;
import com.cinema.dao.IHallDao;
import com.cinema.dao.IMovieDao;
import com.cinema.dao.IMovieSessionDao;
import com.cinema.dao.ISeatsDao;
import com.cinema.dao.ITicketDao;
import com.cinema.dao.IUserDao;
import com.cinema.dao.imp.HallDaoMysql;
import com.cinema.dao.imp.MovieDaoMysql;
import com.cinema.dao.imp.MovieSessionDaoMysql;
import com.cinema.dao.imp.SeatsDaoMysql;
import com.cinema.dao.imp.TicketDaoMysql;
import com.cinema.dao.imp.UserDaoMysql;

public class TicketServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ISeatsDao iSeatsDao = new SeatsDaoMysql();
	IMovieSessionDao iMovieSessionDao = new MovieSessionDaoMysql();
	ITicketDao ticketDao = new TicketDaoMysql();
	IMovieDao iMovieDao = new MovieDaoMysql();
    IUserDao userDao=new UserDaoMysql();
    IHallDao iHallDao = new HallDaoMysql();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	private ITicketDao iTicketDao = new TicketDaoMysql();

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String method = request.getParameter("method");
		// 影票新增
		if ("add".equals(method)) {
			String ticketId = request.getParameter("ticketId");
			String userId = request.getParameter("userId");
			String sessionId = request.getParameter("sessionId");
			String seatId = request.getParameter("seatId");

			Ticket t = new Ticket();
			t.setTicketId(Integer.parseInt(ticketId));
			t.setUserId(userId);
			t.setSessionId(sessionId);

			int insert = iTicketDao.insert(t);

			if (insert == 1) {

				request.getSession().removeAttribute("tlist");
			}
			response.sendRedirect("Ticket/index.jsp");
		} else if ("delete".equals(method)) {
			String ticketId = request.getParameter("ticketId");
			if (ticketId != null && ticketId != "") {

				Ticket ticket = new Ticket();
				ticket.setTicketId(Integer.parseInt(ticketId));

				int delete = iTicketDao.delete(ticket);

				if (delete == 1) {
					response.getWriter().print("success");
					request.getSession().removeAttribute("tlist");
				} else {

					response.getWriter().print("error");
				}
			}
		} else if ("getTicket".equals(method)) {
			String ticketId = request.getParameter("ticketId");

			if (ticketId != null && ticketId != "") {
				Ticket ticket = iTicketDao.SelectById(ticketId);
				request.setAttribute("ticket", ticket);
			}
			request.getRequestDispatcher("/Ticket/edit.jsp").forward(request, response);

		} else if ("update".equals(method)) {
			String ticketId = request.getParameter("ticketId");
			String userId = request.getParameter("userId");
			String sessionId = request.getParameter("sessionId");
			String seatId = request.getParameter("seatId");

			Ticket ticket = new Ticket();
			int update = iTicketDao.update(ticket);
			if (update == 1) {
				request.getSession().removeAttribute("tlist");
			}
			response.sendRedirect("Ticket/index.jsp");
		} else if ("select".equals(method)) {
			request.getSession().removeAttribute("tlist");
			String keyword = request.getParameter("keyword");

			List<Ticket> ticket = iTicketDao.Select(keyword);
			request.getSession().setAttribute("tlist", ticket);
		} else if ("initData".equals(method)) {
			request.setAttribute("movieMessage", request.getParameter("movieMessage"));
			request.getRequestDispatcher("/view/ticket.jsp").forward(request, response);
		} else if ("selectByUser".equals(method)) {
			User user = (User) request.getSession().getAttribute("loginUser");
			String userId = user.getUserId();
			if(userId!=""&&userId!=null){
				List<Map<String, Object>> allTickets = ticketDao.selectByUser(userId);
				
				request.setAttribute("allTickets",allTickets);
				
			}
			request.getRequestDispatcher("/view/userDetail.jsp").forward(request, response);

		}
	}

}
